import router from './router'
import store from './store'

import NProgress from 'nprogress' // progress bar
import 'nprogress/nprogress.css' // progress bar style

import getPageTitle from '@/utils/get-page-title'

const whiteList = ['/login', '/404'] // no redirect whitelist

router.beforeEach(async(to, from, next) => {
  NProgress.start() // 启动进度条
  document.title = getPageTitle(to.meta.title)
  if (!store.state.user.token) { // 无token
    if (whiteList.includes(to.path)) {
      next()
    } else {
      next('/login')
      NProgress.done()
    }
  } else { // 有token
    if (to.path === '/login') {
      next('/')
    } else {
      store.dispatch('user/userSetInfo') // 获取用户信息调用
      next()
    }
  }
})

router.afterEach(() => {
  // finish progress bar
  NProgress.done()
})
